package com.gitee.jmash.websocket.web.listener;

import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.gitee.jmash.core.grpc.DefaultGrpcServer;
import com.gitee.jmash.rbac.client.shiro.JmashClientShiroConfig;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;


/**
 * Web监听器-GRPC服务启动和停止.
 * 
 * @author cgd
 */
@WebListener
public class GrpcServletContextListener implements ServletContextListener {

  private static Log log = LogFactory.getLog(GrpcServletContextListener.class);

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    // Shiro Client 初始化.
    JmashClientShiroConfig.config();
    try {
      final DefaultGrpcServer server = CDI.current().select(DefaultGrpcServer.class).get();
      server.start(false);
    } catch (IOException ex) {
      log.error("", ex);
    }
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
  }

}
